package com.liyao.windowtest4.controller;

import com.liyao.windowtest4.Utils.SharedModel;
import com.liyao.windowtest4.pojo.torqueRow;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import javafx.util.converter.NumberStringConverter;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @program: windowtest4
 * @description:
 * @author: liu yan
 * @create: 2024-10-25 15:37
 */
public class EditTorqueProgramController implements Initializable {

    @FXML
    public TextField TorqueMin;
    public TextField AngleTarget;
    public TextField TorqueMax;
    public TextField AngleMin;
    public TextField TorqueTarget;
    public TextField AngleMax;
    public TextField StartAngle;
    private torqueRow torqueRow;

    public void setDataItem(torqueRow dataItem) {
        this.torqueRow = dataItem;
        TorqueMin.setText(String.valueOf(this.torqueRow.getTorqueMin()));
        AngleTarget.setText(String.valueOf(this.torqueRow.getAngleTarget()));
        TorqueMax.setText(String.valueOf(this.torqueRow.getTorqueMax()));
        AngleMin.setText(String.valueOf(this.torqueRow.getAngleMin()));
        TorqueTarget.setText(String.valueOf(this.torqueRow.getTorqueTarget()));
        AngleMax.setText(String.valueOf(this.torqueRow.getAngleMax()));
        StartAngle.setText(String.valueOf(this.torqueRow.getStartAngle()));
    }

    @FXML
    public void SaveTorque(ActionEvent actionEvent) {
        try {

            boolean isValid = true;

            isValid &= !isTextFieldEmptyOrWhitespace(TorqueMin, "扭矩下限不能为空");
            isValid &= !isTextFieldEmptyOrWhitespace(AngleTarget, "目标角度不能为空");
            isValid &= !isTextFieldEmptyOrWhitespace(TorqueMax, "扭矩上限不能为空");
            isValid &= !isTextFieldEmptyOrWhitespace(AngleMin, "最小角度不能为空");
            isValid &= !isTextFieldEmptyOrWhitespace(TorqueTarget, "目标扭矩不能为空");
            isValid &= !isTextFieldEmptyOrWhitespace(AngleMax, "角度上限不能为空");
            isValid &= !isTextFieldEmptyOrWhitespace(StartAngle, "贴合旧居不能为空");

            if (isValid) {
                this.torqueRow.setTorqueMin(Double.parseDouble(TorqueMin.getText()));
                this.torqueRow.setAngleTarget(Double.parseDouble(AngleTarget.getText()));
                this.torqueRow.setTorqueMax(Double.parseDouble(TorqueMax.getText()));
                this.torqueRow.setAngleMin(Double.parseDouble(AngleMin.getText()));
                this.torqueRow.setTorqueTarget(Double.parseDouble(TorqueTarget.getText()));
                this.torqueRow.setAngleMax(Double.parseDouble(AngleMax.getText()));
                this.torqueRow.setStartAngle(Double.parseDouble(StartAngle.getText()));
                // 关闭当前页面
                Stage stage = (Stage) ((Button) actionEvent.getSource()).getScene().getWindow();
                stage.close();
            }
        } catch (Exception e) {
            // 处理输入错误
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("输入错误");
            alert.setContentText("Please enter valid numbers for torque values.");
            alert.showAndWait();
        }
    }

    private void setNumericTextFormatter(TextField textField) {
        StringConverter<Number> converter = new NumberStringConverter();
        textField.setTextFormatter(new TextFormatter<>(converter, 0, (change) -> {
            if (change.isContentChange()) {
                String text = change.getControlNewText();
                if (text.matches("-?\\d+(\\.\\d*)?|(\\.?)$")) {
                    return change;
                }
            }
            return null; // 拒绝不符合格式的更改
        }));
    }

    private boolean isTextFieldEmptyOrWhitespace(TextField textField, String errorMessage) {
        String text = textField.getText().trim();
        if (text.isEmpty()) {
            showAlert(Alert.AlertType.ERROR,"验证错误", "请填写所有必填项",errorMessage);
            System.err.println(errorMessage);
            return true;
        }
        return false;
    }

    private void showAlert(Alert.AlertType alertType, String title, String headerText,String ContentText) {
        Alert alert = new Alert(alertType);
        alert.setTitle(title);
        alert.setHeaderText(headerText);
        alert.setContentText(ContentText); // 可以根据需要自定义内容文本
        alert.showAndWait(); // 显示对话框并等待用户响应
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        setNumericTextFormatter(TorqueMin);
        setNumericTextFormatter(AngleTarget);
        setNumericTextFormatter(TorqueMax);
        setNumericTextFormatter(AngleMin);
        setNumericTextFormatter(TorqueTarget);
        setNumericTextFormatter(AngleMax);
        setNumericTextFormatter(StartAngle);
    }
}
